package edu.pku.sei.transformation.editor.views.tree;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

import edu.pku.sei.modeler.model.core.MetaClass;
import edu.pku.sei.modeler.model.core.MetaClassifier;
import edu.pku.sei.modeler.model.core.MetaModel;
import edu.pku.sei.modeler.model.core.MetaPackage;

public class ModelTreeViewerContentProvider implements ITreeContentProvider {

	public Object[] getChildren(Object parentElement) {
		if (parentElement instanceof MetaModel) {
			MetaModel tree = (MetaModel) parentElement;
			return tree.getPackages().toArray();
		} else if (parentElement instanceof MetaPackage) {
			ArrayList list = new ArrayList();
			MetaPackage pkg = (MetaPackage) parentElement;
			list.addAll(pkg.getSubPackages());
			list.addAll(pkg.getNamedElements());
			return list.toArray();
		} else if (parentElement instanceof MetaClass) {
			ArrayList list = new ArrayList();
			MetaClass cls = (MetaClass) parentElement;
			list.addAll(cls.getGenerals());
			list.addAll(cls.getAllAttributes());
			list.addAll(cls.getReferences());

			return list.toArray();
		} else if (parentElement instanceof MetaClassifier) {
			return new Object[0];
		} else {
			return new Object[0];
		}
	}

	public Object getParent(Object element) {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean hasChildren(Object element) {
		if (element instanceof MetaModel) {
			MetaModel model = (MetaModel) element;
			if (model.getPackages().size() != 0)
				return true;
			else
				return false;
		} else if (element instanceof MetaPackage) {
			MetaPackage pkg = (MetaPackage) element;
			if (pkg.getSubPackages().size() != 0
					|| pkg.getNamedElements().size() != 0)
				return true;
			else
				return false;
		} else if (element instanceof MetaClass) {
			MetaClass cls = (MetaClass) element;
			if (cls.getAttributes().size() != 0
					|| cls.getReferences().size() != 0) {
				return true;
			} else {
				return false;
			}
		} else if (element instanceof MetaClassifier) {
			return false;
		} else {
			return false;
		}
	}

	public Object[] getElements(Object inputElement) {
		if (inputElement instanceof List) {
			return ((List) inputElement).toArray();
		} else {
			return new Object[0];
		}
	}

	public void dispose() {
		// TODO Auto-generated method stub

	}

	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		// TODO Auto-generated method stub

	}

}
